package com.fy56.platform.logistics.utils;

import java.util.concurrent.ConcurrentLinkedQueue;

/**
 * Created by lilianqiang on 2015-12-14.
 */
public class IdWorkerTools {
    // 利用静态内部类特性实现外部类的单例
    private static IdWorker idWorker;
    private static ConcurrentLinkedQueue ids = new ConcurrentLinkedQueue();

    private static IdWorkerTools singleTon=new IdWorkerTools();

    // 私有化构造函数
    private IdWorkerTools() {
         idWorker=new IdWorker();
    }

    public static IdWorkerTools getInstance() {
        return singleTon;
    }

    public String getId() {
        if(ids.isEmpty()){
            for(int i=0;i<10000;i++){
                ids.add(idWorker.getId());
            }
        }
        Long id= (Long)ids.poll();
        return String.valueOf(id);
    }

    public static void main(String[] args) {
        for(int i=0;i<10000000;i++) {
            try {
                Thread.sleep(1000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            System.out.println("@@@@");
            System.out.println(IdWorkerTools.getInstance().getId());
        }
    }
}
